﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using mana.lib.rates.service;
using MailBodyFormatter;
using DDay.iCal;
using Calendar;
using DDay.iCal.DataTypes;
using System.Xml;
using System.Net;
using System.ServiceModel.Syndication;
using System.Xml.Linq;
using System.Linq;
using DDay.iCal.Components;
namespace EmailResponseByParsing
{
    public partial class EmailResonseParser : Form
    {
        iCalendar _bookings = HACalendar.GetCal();

        public EmailResonseParser()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool booked = false;
            Parser.ParseRequest target = new Parser.ParseRequest();
            string strRequest = this.Request.Text;

            RequestParser.Request actual;
            actual = target.Parse(strRequest);

            StringBuilder sbLog = new StringBuilder();

            Rates rtTable = RatesInitializer.GetInitializedRates();
            int numNights = actual.NumNights;
            double rate = 0;
            List<DDay.iCal.Components.Occurrence> occ = _bookings.GetOccurrences((iCalDateTime)actual.ArrivalDate.AddDays(-30), (iCalDateTime)actual.ArrivalDate.AddDays(30));

            for (int i = 0; i < numNights; i++)
            {

                Decimal x = rtTable.GetRateForDate(actual.ArrivalDate.AddDays(i));
           
                if (!CheckAvailability(occ, actual.ArrivalDate.AddDays(i)))
                {
                    booked = true;
                    sbLog.AppendFormat("{0} is booked - rate {1}\r\n", actual.ArrivalDate.AddDays(i), x);
                }
                else
                {
                    sbLog.AppendFormat("{0} available - rate {1}\r\n", actual.ArrivalDate.AddDays(i), x);
                }

                rate += (float)x;
            }

            double tax = (double)rate * 0.10675;
            this.Response.Text = sbLog.ToString() +  MailBody.GetFormattedBody(actual.WebAddress, actual.Name, actual.NumInParty, rate, tax, numNights,actual.ArrivalDate,booked);
        }

        private bool CheckAvailability(List<DDay.iCal.Components.Occurrence> occList, DateTime dateTime)
        {
            DateTime chkDtTm = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 19, 0, 0);
            foreach (Occurrence occ in occList)
            {
                if (occ.Period.StartTime <= chkDtTm && occ.Period.EndTime >= chkDtTm)
                {
                    return false;
                }

            }
            return true;
        }

       

        private void button2_Click(object sender, EventArgs e)
        {


            GetGMail();

            //GmailItems.DataSource = items;
     //       foreach (var item in items)
     //       {
      //          GmailItems.Items.Add(item.Summary);
      //      }



        }

        private static void GetGMail()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            XmlUrlResolver resolver = new XmlUrlResolver();
            resolver.Credentials = new NetworkCredential("Amy", "kana1234");
            settings.XmlResolver = resolver;
            XmlReader reader = XmlReader.Create("https://mail.google.com/mail/feed/atom", settings);
            XNamespace ns = XNamespace.Get("http://purl.org/atom/ns#");
            XDocument feed = XDocument.Load(reader);
            var items = from item in feed.Descendants(ns + "entry")
                        select new

                        {
                            Author = item.Element(ns + "author").Element(ns + "name").Value,
                            Title = item.Element(ns + "title").Value,
                            Link = item.Element(ns + "link").Attribute("href").Value,
                            Summary = item.Element(ns + "summary").Value
                        };
        }
    }
}
